var faqCategoryId = url('?faqCategoryId');
var isTrue;
var page = 1
var myScroll,pullUpEl, pullUpOffset= 0;
QCategory()

		//获取百科分类
	function QCategory(){
		$.ajax({
			type:"GET",
			url:AJAXURL+"/100/15/1001502",
			dataType: AJAXDATATYPE,
			contentType:AJAXCONTENTTYPE,
			beforeSend: function(){
	        },
	        success:function(data){
	        		console.log(data.result)
	      		if(data.success==true){
	      			$.each(data.result, function(i,v) {
		        			var html = '<li data-dataId="'+v.dataId+'"><img src="'+v.categoryThumb+'"/><p>'+v.categoryName+'</p></li>'
		        			$(html).prependTo('.qa_category_ul')
		        		});
		        		console.log('hei',$(window).height(),$('.qa_category').height())
		        		$('#k_main').height($(window).height()-$('.qa_category').outerHeight()-30)
		        		QList({
						"faqCategoryId": faqCategoryId,
						"pageNum": 1,
						"pageSize": 20
					})
		        		$('.qa_category_ul li').each(function(i,v){
		        			if(faqCategoryId==$('.qa_category_ul li').eq(i).attr('data-dataId')){
		        				$(this).addClass('choice').siblings().removeClass('choice')
		        			}
		        			$(this).click(function(){
		        				$(this).addClass('choice').siblings().removeClass('choice')
		        				faqCategoryId = $(this).attr('data-dataId')
		        				page = 1
		        				$('.qa_list .qa_item').remove()
		        				setTimeout(function(){
		        					QList({
							    "faqCategoryId": faqCategoryId,
								"pageNum": 1,
								"pageSize": 20
							})
		        				},200)
		        			})
		        		})
	      		}

	        },
	        error:function(){
	        		console.log('error')
	        }
		});
	}

	//获取百科列表
	function QList(param){
		$.ajax({
			type:AJAXTYPE,
			url:AJAXURL+"/100/15/1001506",
			data:JSON.stringify(param),
			dataType: AJAXDATATYPE,
			contentType:AJAXCONTENTTYPE,
			beforeSend: function(){
	        },
	        success:function(data){
	        		console.log(data.result.records)
	        		if(data.success==true){
	        			$.each(data.result.records,function(i,v){
	        				var html = ['<div class="qa_item" data-dataId="'+v.id+'">',
									'	<p class="qa_item_title">'+v.faqQuestion+'</p>',
									'</div>'
									].join('')
						$(html).appendTo('.qa_list')
	        			})
	        			if(data.result.current<data.result.pages){
	        				isTrue = true
	        				page++
	        				$('.pullUpLabel').text('上拉加载更多')
	        			}else if(data.result.current==data.result.pages){
	        				isTrue = false
						$('.pullUpLabel').text('全部数据加载完成')

					}
	      			$('.qa_item').each(function(){
	      				$(this).click(function(){
								window.location.href= './QArticle.html?id='+$(this).attr('data-dataId')
	      				})
	      			})
	        		}

	        },
	        error:function(){
	        		console.log('error')
	        }
		});
	}

	function loaded() {
		//动画部分
		var useTransition = true
			if( is_android_x5_core() )
				useTransition = false


		pullUpEl = document.getElementById('getmore');
		pullUpOffset = pullUpEl.offsetHeight;
		myScroll = new iScroll('k_main', {
			useTransition: useTransition,
			vScroll:true,
			checkDOMChanges:true,
			onRefresh: function () {
				if (pullUpEl.className.match('loading')) {
					pullUpEl.className = '';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多';
				}
			},
			onScrollMove: function () {
				if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
					pullUpEl.className = 'flip';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中';
					this.maxScrollY = this.maxScrollY;
				} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
					pullUpEl.className = '';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
					this.maxScrollY = pullUpOffset;
				}
			},
			onScrollEnd: function () {
				if (pullUpEl.className.match('flip')) {
					pullUpEl.className = 'loading';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中';
					pullUpAction();	// Execute custom function (ajax call?)
				}
			}
		});

		loadAction();
	}
	//document.addEventListener('touchmove', function (e) { e.preventDefault(); }, { passive: false });//阻止冒泡
	document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 0); }, { passive: false });

	//初始状态，加载数据
	function loadAction(){
		var el, li;
		el = document.getElementById('getmore');
//		QList({
//			"faqCategoryId": faqCategoryId,
//			"pageNum": 1,
//			"pageSize": 20
//		})

		myScroll.refresh();
	}
	//上拉加载更多数据
	function pullUpAction () {
		setTimeout(function () {
			var el, li;
			el = document.getElementById('getmore');
	//		----
		if(isTrue==true){
			QList({
				"faqCategoryId": faqCategoryId,
				"pageNum": page,
				"pageSize": 20
			})
			myScroll.refresh();
		}else{
			isTrue = false
			$('.pullUpLabel').text('全部数据加载完成')
		}

	//		----

		}, 400);
	}
